package base;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import FlieManager.GetItFromFile;
public class ChangeEncoding {

		
		public void change(String file,String sorceE,String toE) throws FileNotFoundException, UnsupportedEncodingException{
		PrintStream old = System.out;
				System.setOut(new PrintStream(file+"."+toE,toE));
			ArrayList<String> lista = new ArrayList<String>();
			GetItFromFile atest = new GetItFromFile();
			lista = atest.GetArrayList("",file, sorceE);
			for(int i=0;i<lista.size();i++)
				System.out.println(lista.get(i));	
		System.setOut(old);
		}
		
		public void change(String dirfile,String todir,String sourceE,String toE) throws FileNotFoundException, UnsupportedEncodingException{
			File dir = new File(dirfile);
			File files[] = dir.listFiles();
			GetItFromFile atest = new GetItFromFile();
			PrintStream old = System.out;
			for(int i = 0; i < files.length; i++){
				String pfilename = files[i].getName();
				System.setOut(new PrintStream(todir+"//"+pfilename+"."+toE,toE));
				ArrayList<String> lista = new ArrayList<String>();
				lista = atest.GetArrayList("",files[i].getAbsolutePath(), sourceE);	
				for(int lineno=0;lineno<lista.size();lineno++)
					System.out.println(lista.get(lineno));	
			}
			System.setOut(old);
		}
		
		public static void main(String args[]) throws FileNotFoundException, UnsupportedEncodingException{
			
					
			if(args.length<1||args.length>4){
				System.out.println("WrongInfo!  Filename  sorce-encoding to-encoding ");
				System.exit(0);
			}
			
			System.out.println(args.length+ "\t"+ args[0]);		
			ChangeEncoding ac = new ChangeEncoding();
			if(args.length==3){

		 	ac.change(args[0], args[1], args[2]);
			}
		 	if(args.length==4)
		 		ac.change(args[0], args[1], args[2],args[3]);
		}
}


